package com.yumeng.framework.auth.security.configurers;

import com.yumeng.framework.auth.security.authentication.normal.NormalLoginAuthenticationFilter;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

/**
 * 普通登录配置
 *
 * @author wxd
 * @date 2025/3/13 16:18
 */
public final class NormalLoginConfigurer<H extends HttpSecurityBuilder<H>> extends
        MyAbstractAuthenticationFilterConfigurer<H, NormalLoginConfigurer<H>, NormalLoginAuthenticationFilter> {

    private final NormalLoginAuthenticationFilter filter;

    private NormalLoginConfigurer(NormalLoginAuthenticationFilter filter){
        super(filter, null);
        this.filter = filter;
        usernameParameter("username");
        passwordParameter("password");
        clientTypeParameter("clientType");
    }

    @Override
    public NormalLoginConfigurer<H> loginPage(String loginPage) {
        return super.loginPage(loginPage);
    }

    public NormalLoginConfigurer<H> usernameParameter(String usernameParameter) {
        getAuthenticationFilter().setUsernameParameter(usernameParameter);
        return this;
    }

    public NormalLoginConfigurer<H> passwordParameter(String passwordParameter) {
        getAuthenticationFilter().setPasswordParameter(passwordParameter);
        return this;
    }

    @Override
    public void init(H http) throws Exception {
        super.init(http);
    }

    @Override
    protected RequestMatcher createLoginProcessingUrlMatcher(String loginProcessingUrl) {
        return PathPatternRequestMatcher.withDefaults().matcher(HttpMethod.POST, loginProcessingUrl);
    }

    public static NormalLoginConfigurer<HttpSecurity> normalLogin(){
        NormalLoginAuthenticationFilter filter = new NormalLoginAuthenticationFilter();
        return new NormalLoginConfigurer<>(filter);
    }
}
